package com.example.zhangguoqing.homeofpet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.telecom.Call;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.zhangguoqing.homeofpet.model.DataDao;
import com.example.zhangguoqing.homeofpet.utils.HttpUtil;
import com.example.zhangguoqing.homeofpet.utils.JsonUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Response;

public class DataFragment extends Fragment {
    Button imageButton;
    TextView getTemp;
    TextView getWet;
    TextView getEC;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_data, null);
        imageButton = (Button) view.findViewById(R.id.button);
        getTemp=view.findViewById(R.id.getTemp);
        getWet=view.findViewById(R.id.getWet);
        getEC=view.findViewById(R.id.getEC);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://10.0.2.2:8080/user/register?name=3000lux&password=80%25";

                HttpUtil.sendOkHttpRequest(url, new okhttp3.Callback() {

                    @Override
                    public void onFailure(okhttp3.Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(okhttp3.Call call, Response response) throws IOException {
                        String responseData = response.body().string();
                        Log.i("log",responseData);
                        showResponse(responseData);
                    }




                });
            }
        });
        return view;
    }
    private void showResponse(final String response){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //进行UI操作，将结果展示到界面上
                List<DataDao> users= JsonUtil.parseArrayJsonWithGson(response);
                DataDao userPO=new DataDao();
                if(!users.isEmpty()){
                    userPO=users.get(0);
                    getWet.setText(userPO.getGuangzhao());
                    getTemp.setText(userPO.getKongqi());
                    getEC.setText("2.6mmhos/cm");
                }

            }
        });
    }

    public void setMenuVisibility(boolean menuVisibile) {
        super.setMenuVisibility(menuVisibile);
        if (this.getView() != null) {
            this.getView().setVisibility(menuVisibile ? View.VISIBLE : View.GONE);
        }
    }


}
